/**
 * Title:        字段属性检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;



public class WorkflowNodeflowCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("HtmlInfoCheck  start");

        List resultList = new ArrayList();
        String result = "";
        int cnt = getDataCount();
        if(cnt > 0){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage", cnt + "条流程存在问题，需要修复。");
            resultMap.put("buttonname","修复数据");
            resultList.add(resultMap);
            
        }else{
            result = resultOk;
        }
        (new BaseBean()).writeLog("HtmlInfoCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();

        List resultList = new ArrayList();
        String result = "";
        if(dataMap!= null && dataMap.get("divid")!= null){
            //根据参数获取列表
            String divid =  (String)dataMap.get("divid");

            fixdataDetail();

            int cnt = getDataCount();
            if(cnt > 0){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("fixMessage","修复失败");
                resultMap.put("divid",divid);
                resultMap.put(result,resultNeedFix);
                resultList.add(resultMap);
            }else{
                result = resultOk;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("fixMessage","修复成功");
                resultMap.put("divid",divid);
                resultMap.put(result,resultOk);
                resultList.add(resultMap);
            }
            return formatReturnJsonFix(result, resultList,this);
        }else{
            fixdataDetail();
            return checkData(dataMap);
        }
        
        
    }

    @Override
    public String getClassName() {
        return "字段属性检测";
    }

    @Override
    public String getClassDesc() {
        return "检测字段属性设定错误(必填，只读，可编辑属性不正确)）";
    }

    private int getDataCount(){

        String sql = "";

        sql += "select count(1) as cnt from (select count(1) as cnt ";
        //必填属性与只读可编辑属性不匹配
        sql += "  from (select '必填属性与只读可编辑属性不匹配' as str,1 as flag, nodeid, fieldid ";
        sql += "          from workflow_nodeform ";
        sql += "         where (ismandatory = '1' and (isview = '0' or isedit = '0')) ";
        sql += "        union ";
        //只读属性与可编辑属性不匹配
        sql += "        select '只读属性与可编辑属性不匹配' as str,2 as flag, nodeid, fieldid ";
        sql += "          from workflow_nodeform ";
        sql += "         where (isedit = '1' and isview = '0') ";
        sql += "        union ";
        //必填属性为空
        sql += "        select '必填属性为空' as str,3 as flag, nodeid, fieldid ";
        sql += "          from workflow_nodeform ";
        sql += "         where (ismandatory is null or ismandatory = '') ";
        sql += "        union ";
        //只读属性为空
        sql += "        select '只读属性为空' as str,4 as flag, nodeid, fieldid ";
        sql += "          from workflow_nodeform ";
        sql += "         where (isview is null or isview = '') ";
        sql += "        union ";
        //可编辑属性为空
        sql += "        select '可编辑属性为空' as str,5 as flag, nodeid, fieldid ";
        sql += "          from workflow_nodeform ";
        sql += "         where (isedit is null or isedit = '')) errormsg ";
        sql += " inner join workflow_nodebase ";
        sql += "    on errormsg.nodeid = workflow_nodebase.id ";
        sql += " inner join workflow_flownode ";
        sql += "    on workflow_flownode.nodeid = workflow_nodebase.id ";
        sql += " inner join workflow_base ";
        sql += "    on workflow_flownode.workflowid = workflow_base.id ";
        sql += " inner join workflow_billfield ";
        sql += "    on workflow_billfield.id = errormsg.fieldid ";
        sql += "  left join HtmlLabelInfo ";
        sql += "    on HtmlLabelInfo.indexid = workflow_billfield.fieldlabel ";
        sql += "   and languageid = 7 ";
        sql += " group by workflow_base.id) aaa";
        recordSet.executeSql(sql);
        if(recordSet.next()){
            return recordSet.getInt("cnt");
        }else{
            return 0;
        }
    }
    private void fixdataDetail(){

         RecordSet rs = new RecordSet();
         //可编辑属性为空
         String strSql = "update workflow_nodeform set isedit ='0'  where (isedit is null or isedit = '') ";
         rs.executeUpdate(strSql);
         //只读属性为空
         strSql = "update workflow_nodeform set isview ='0'  where (isview is null or isview = '') ";
         rs.executeUpdate(strSql);
         //必填属性为空
         strSql = "update workflow_nodeform set ismandatory ='0'  where (ismandatory is null or ismandatory = '') ";
         rs.executeUpdate(strSql);
         //只读属性与可编辑属性不匹配
         strSql = "update workflow_nodeform set isview ='1'  where (isedit = '1' and isview = '0') ";
         rs.executeUpdate(strSql);
         //必填属性与只读可编辑属性不匹配
         strSql = "update workflow_nodeform set isview ='1',isedit ='1' where (ismandatory = '1' and (isview = '0' or isedit = '0')) ";
         rs.executeUpdate(strSql);
    }
}



